home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////////////////////////////////////////////////////////
- //
- // File: DrawingUtils.cp
- //
- // Project: MacHack 2000 - Vertigo!
- // Authors: Darrin Cardani, Drew Thaler, Ed Wynne
- //
- // Date: 06/23/2000 (written entirely during the conference!)
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- #define DISABLE_LOCAL_CALLTRACE 0
- #define DISABLE_LOCAL_DEBUG 0
- #include "DebugUtils.h"
-
- #include "DrawingUtils.h"
-
-
-
- // ---------------------------------------------------------------------------
- // RegionList
- // ---------------------------------------------------------------------------
- RegionList::RegionList()
- {
- mArrayStorage = NewHandleSys(8);
- dAssert(mArrayStorage != NULL);
- HLock(mArrayStorage);
- mArray = (*(RgnHandle**)mArrayStorage);
-
- mNull = NULL;
- mItemsInArray = 0;
- mCurrentArrayCapacity = 2;
- }
-
- RegionList::~RegionList()
- {
- if (mArrayStorage != NULL) DisposeHandle(mArrayStorage); mArrayStorage = NULL;
- mItemsInArray = 0;
- mCurrentArrayCapacity = 0;
- }
-
- // ---------------------------------------------------------------------------
- // RegionList::Add
- // ---------------------------------------------------------------------------
-
- void
- RegionList::Add(RgnHandle rgn, int duplicateRgn)
- {
- dAssert(mArrayStorage != NULL);
- dAssert(rgn != NULL);
- if ((mArrayStorage == NULL) || (rgn == NULL))
- return;
-
- // Duplicate the region if we've been told.
- if (duplicateRgn)
- {
- RgnHandle tempRgn = NewRgn();
- CopyRgn(rgn,tempRgn);
- rgn = tempRgn;
- }
-
- // Do the easy case, just slam into the end of the array.
- if (mItemsInArray < mCurrentArrayCapacity)
- {
- mArray[mItemsInArray++] = rgn;
- return;
- }
-
- // Grow the handle since we need more space.
- HUnlock(mArrayStorage);
- SetHandleSize(mArrayStorage,2*sizeof(RgnHandle)*mCurrentArrayCapacity);
- HLock(mArrayStorage);
-
- // Sanity check to make sure the handle really grew.
- mArray = (*(RgnHandle**)mArrayStorage);
- mCurrentArrayCapacity = GetHandleSize(mArrayStorage) / sizeof(RgnHandle);
-
- dAssert(mItemsInArray < mCurrentArrayCapacity);
- if (mItemsInArray < mCurrentArrayCapacity)
- mArray[mItemsInArray++] = rgn;
- else
- dprintf(kDConPrefix "RegionList::Add - SetHandleSize failed! MemErr = %ld\n", (long)MemError());
- }
-
-
- // ---------------------------------------------------------------------------
- // RegionList::Remove
- // ---------------------------------------------------------------------------
-
- void
- RegionList::Remove(RgnHandle rgn, int dispose)
- {
- dAssert(mArrayStorage != NULL);
- dAssert(rgn != NULL);
- if ((mArrayStorage == NULL) || (rgn == NULL))
- return;
-
- // Walk through the list looking for the region.
- for (int i=0; i<mItemsInArray; ++i)
- {
- if ((rgn == mArray[i]) || (EqualRgn(rgn,mArray[i])))
- {
- // Got it. If we're disposing then do it now.
- if (dispose) DisposeRgn(mArray[i]);
-
- // Shift the array down.
- mItemsInArray--;
- BlockMoveData(&mArray[i+1],&mArray[i],(mItemsInArray-i)*sizeof(RgnHandle));
- return;
- }
- }
-
- // Didn't find it. print out a message for debugging and return.
- dprintf(kDConPrefix "RegionList::Remove - rgn $%08X is not in list!\n", (int)rgn);
- }
-
-
- // ---------------------------------------------------------------------------
- // RegionList::RemoveAll
- // ---------------------------------------------------------------------------
-
- void
- RegionList::RemoveAll(int dispose)
- {
- if (dispose)
- for (int i=0; i<mItemsInArray; ++i)
- DisposeRgn(mArray[i]);
-
- mItemsInArray = 0;
- }
-
-
- // ---------------------------------------------------------------------------
- // RegionList::operator[]
- // ---------------------------------------------------------------------------
-
- RgnHandle &
- RegionList::operator[] (UInt32 index)
- {
- mNull = NULL;
- dAssert(mArrayStorage != NULL);
- if (mArrayStorage == NULL)
- return mNull;
-
- // Grow the array in response to invalid indices. This
- // is deliberate, so that we don't have to worry about hitting
- // a bad index when re-ordering windows in the list.
- while (index > mCurrentArrayCapacity)
- {
- dprintf(kDConPrefix "Growing array to %ld items\n", 2*mCurrentArrayCapacity);
-
- HUnlock(mArrayStorage);
- SetHandleSize(mArrayStorage,2*sizeof(RgnHandle)*mCurrentArrayCapacity);
- HLock(mArrayStorage);
- if (MemError()) return mNull;
-
- mArray = (*(RgnHandle**)mArrayStorage);
- mCurrentArrayCapacity = GetHandleSize(mArrayStorage) / sizeof(RgnHandle);
- }
-
- // Okay, return it.
- return mArray[index];
- }
-
-
-
- #pragma mark -
-
-
-
- // ---------------------------------------------------------------------------
- // StGWorld::Create(Rect)
- // ---------------------------------------------------------------------------
- // Why would you ever want anything but a 32-bit GWorld?
- //
- void
- StGWorld::Create(const Rect &inRect)
- {
- OSStatus err = NewGWorld(&mGWorld,32,&inRect,NULL,NULL,useTempMem | keepLocal | pixelsLocked);
- if (err != noErr) {
- dprintf(kDConPrefix "GWorld allocation (%dx%d) failed! err = %ld\n",
- (inRect.right - inRect.left), (inRect.bottom - inRect.top), err);
- mGWorld = NULL;
- return;
- }
- }
-
-
-